package com.bwlcums.utils;

import javax.servlet.http.HttpServletRequest;

public class StringUtils {
	
	public static boolean isNotNullOrEmpty(String str){
		if(str!=null && !str.equals("") && !str.equals("null")){
			return true;
		}
		return false;
	}
	
	/**
	 * 
	  * 
	  * 文件大小转换
	  * 
	  *
	  * @author lixl
	  * @date 2016年5月27日${time}
	  * @param size
	  * @return
	 */
	public static String convertFileSize(long size) {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;
 
        if (size >= gb) {
            return String.format("%.1f GB", (float) size / gb);
        } else if (size >= mb) {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (size >= kb) {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } else
            return String.format("%d B", size);
    }
	
	/**
	 * 
	 * @param request
	 * @return
	 */
	public static String getRemortIP(HttpServletRequest request) {
		String ip = request.getParameter("ip");
		if(ip==null || "".equals(ip)){
			if (request.getHeader("x-forwarded-for") == null) {
				ip = request.getRemoteAddr();
			}else{
				ip = request.getHeader("x-forwarded-for");
			}
		}
		
		return ip;
	}

}
